﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>$CreationDate$</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;

namespace Outcoder.Collections
{
	public static class DictionaryExtensions
	{
		/// <summary>
		/// This method returns a strongly typed object from a dictionary of objects.
		/// </summary>
		/// <typeparam name="T">The actual type of the item in the dictionary.</typeparam>
		/// <param name="dictionary">The dictionary to retrieve the item from.</param>
		/// <param name="key">The string key of the item in the dictionary.</param>
		/// <param name="value">The item in the dictionary with the matching key.</param>
		/// <returns><c>true</c> if the item with the key was found; <c>false</c> otherwise.</returns>
		/// <exception cref="InvalidCastException">Occurs if the object in the dictionary is not of the specified type.</exception>
		public static bool TryGetValue<T>(this IDictionary<string, object> dictionary, string key, out T value)
		{
			object item;
			if (dictionary.TryGetValue(key, out item))
			{
				value = (T)item;
				return true;
			}

			value = default(T);

			return false;
		}
	}
}
